﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static MyDelegateDemo.Linq.RichardLinq;

namespace MyDelegateDemo.Linq
{
    /// <summary>
    /// Linq
    /// </summary>
    public class LinqShow
    {
        #region Data Init
        private static List<Student> GetStudentList()
        {
            #region 初始化数据
            List<Student> studentList = new List<Student>()
            {
                new Student()
                {
                    Id=1,
                    Name="赵亮",
                    ClassId=2,
                    Age=35
                },
                new Student()
                {
                    Id=1,
                    Name="再努力一点",
                    ClassId=2,
                    Age=23
                },
                 new Student()
                {
                    Id=1,
                    Name="王炸",
                    ClassId=2,
                    Age=27
                },
                 new Student()
                {
                    Id=1,
                    Name="疯子科学家",
                    ClassId=2,
                    Age=26
                },
                new Student()
                {
                    Id=1,
                    Name="灭",
                    ClassId=2,
                    Age=25
                },
                new Student()
                {
                    Id=1,
                    Name="黑骑士",
                    ClassId=2,
                    Age=24
                },
                new Student()
                {
                    Id=1,
                    Name="故乡的风",
                    ClassId=2,
                    Age=21
                },
                 new Student()
                {
                    Id=1,
                    Name="晴天",
                    ClassId=2,
                    Age=22
                },
                 new Student()
                {
                    Id=1,
                    Name="旭光",
                    ClassId=2,
                    Age=34
                },
                 new Student()
                {
                    Id=1,
                    Name="oldkwok",
                    ClassId=2,
                    Age=30
                },
                new Student()
                {
                    Id=1,
                    Name="乐儿",
                    ClassId=2,
                    Age=30
                },
                new Student()
                {
                    Id=1,
                    Name="暴风轻语",
                    ClassId=2,
                    Age=30
                },
                new Student()
                {
                    Id=1,
                    Name="一个人的孤单",
                    ClassId=2,
                    Age=28
                },
                new Student()
                {
                    Id=1,
                    Name="小张",
                    ClassId=2,
                    Age=30
                },
                 new Student()
                {
                    Id=3,
                    Name="阿亮",
                    ClassId=3,
                    Age=30
                },
                  new Student()
                {
                    Id=4,
                    Name="37度",
                    ClassId=4,
                    Age=30
                }
                  ,
                  new Student()
                {
                    Id=4,
                    Name="关耳",
                    ClassId=4,
                    Age=30
                }
                  ,
                  new Student()
                {
                    Id=4,
                    Name="耳机侠",
                    ClassId=4,
                    Age=30
                },
                  new Student()
                {
                    Id=4,
                    Name="Wheat",
                    ClassId=4,
                    Age=30
                },
                  new Student()
                {
                    Id=4,
                    Name="Heaven",
                    ClassId=4,
                    Age=22
                },
                  new Student()
                {
                    Id=4,
                    Name="等待你的微笑",
                    ClassId=4,
                    Age=23
                },
                  new Student()
                {
                    Id=4,
                    Name="畅",
                    ClassId=4,
                    Age=25
                },
                  new Student()
                {
                    Id=4,
                    Name="混无痕",
                    ClassId=4,
                    Age=26
                },
                  new Student()
                {
                    Id=4,
                    Name="37度",
                    ClassId=4,
                    Age=28
                },
                  new Student()
                {
                    Id=4,
                    Name="新的世界",
                    ClassId=4,
                    Age=30
                },
                  new Student()
                {
                    Id=4,
                    Name="Rui",
                    ClassId=4,
                    Age=30
                },
                  new Student()
                {
                    Id=4,
                    Name="帆",
                    ClassId=4,
                    Age=30
                },
                  new Student()
                {
                    Id=4,
                    Name="肩膀",
                    ClassId=4,
                    Age=30
                },
                  new Student()
                {
                    Id=4,
                    Name="孤独的根号三",
                    ClassId=4,
                    Age=30
                },
                  new Student()
                {
                    Id=4,
                    Name="振轩儿",
                    ClassId=4,
                    Age=30
                }
            };
            #endregion
            return studentList;
        }
        #endregion


        /// <summary>
        /// 以下全部都是有Where后，简单操作；
        /// 假设：没有这个Where方法呢？
        /// 
        /// </summary>

        public static void Show()
        {
            List<Student> studentList = GetStudentList();
            //1.名称包含赵亮
            {
                var sList = studentList.Where(s => s.Name.Contains("赵亮")).ToList();

                #region MyRegion
                //List<Student> students1 = new List<Student>(); //经过计算，这个集合和sList结果一致的；
                //foreach (var student in studentList)
                //{
                //    //if (student.Name.Contains("赵亮"))
                //    if (IsOk(student))
                //    {
                //        students1.Add(student);
                //    }
                //}

                //////DoWhere doWhere = IsOk;
                ////DoWhere doWhere = student => student.Name.Contains("赵亮");
                ////List<Student> students2 = RichardLinq.RichardWhere(studentList, doWhere);

                //List<Student> students3 = RichardLinq.RichardWhere(studentList, student => student.Name.Contains("赵亮"));
                #endregion

                List<Student> students4 = studentList.RichardWhere(s => s.Name.Contains("赵亮"));

            }
            //2.筛选中集合中名称的长度小于4的----Where
            {
                var sList = studentList.Where(s => s.Name.Length < 4).ToList();
                 
                #region MyRegion


                //List<Student> students1 = new List<Student>(); //经过计算，这个集合和sList结果一致的；
                //foreach (var student in studentList)
                //{
                //    //if (student.Name.Length < 4)
                //    if (IsOk1(student))
                //    {
                //        students1.Add(student);
                //    }
                //}

                //////DoWhere doWhere = IsOk1;
                ////DoWhere doWhere = student => student.Name.Length < 4;
                ////List<Student> students2 = RichardLinq.RichardWhere(studentList, doWhere);

                //List<Student> students3 = RichardLinq.RichardWhere(studentList, student => student.Name.Length < 4);
                #endregion

                List<Student> students4 = studentList.RichardWhere(s => s.Name.Length < 4);

            }
            //3. N个条件叠加，随着需求的不同，我根本不知道究竟需要多个个条件；  
            {
                var sList = studentList.Where(item => item.Id > 1
                            && item.Name != null
                            && item.ClassId > 2
                            && item.Age > 20).ToList(); 
                #region MyRegion
                 
                //List<Student> students1 = new List<Student>(); //经过计算，这个集合和sList结果一致的；
                //foreach (var item in studentList)
                //{
                //    //if (item.Id > 1
                //    //        && item.Name != null
                //    //        && item.ClassId > 2
                //    //        && item.Age > 20)
                //    if (IsOk2(item))
                //    {
                //        students1.Add(item);
                //    }
                //}
                ////DoWhere doWhere = IsOk2;
                //DoWhere doWhere = item=> item.Id > 1
                //        && item.Name != null
                //        && item.ClassId > 2
                //        && item.Age > 20;
                //List<Student> students2 = RichardLinq.RichardWhere(studentList, doWhere);


                //List<Student> students3 = RichardLinq.RichardWhere(studentList, item => item.Id > 1
                //        && item.Name != null
                //        && item.ClassId > 2
                //        && item.Age > 20);
                #endregion

                List<Student> students4 = studentList.RichardWhere(item => item.Id > 1
                            && item.Name != null
                            && item.ClassId > 2
                            && item.Age > 20);
            }

        }








        private static bool IsOk(Student student)
        {
            return student.Name.Contains("赵亮");
        }

        private static bool IsOk1(Student student)
        {
            return student.Name.Length < 4;
        }

        private static bool IsOk2(Student item)
        {
            return item.Id > 1
                        && item.Name != null
                        && item.ClassId > 2
                        && item.Age > 20;
        }
    }
}

